package com.zenaprod.zenda.bag;

import com.zenaprod.framework.log.ZLog;
import com.zenaprod.twodcore.graphic.element.Element;
import com.zenaprod.zenda.config.Config;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;


public class BagModel extends Observable {
    private static int id = 0;

    private String name;
    private int bagId, maxSize, weight;
    private int emptyBox;
    private List<Box> boxes;

    private int rowCount = 0;

    public BagModel(int maxSize, int weight) {
        this.maxSize = maxSize;
        this.weight = weight;
        this.boxes = new ArrayList<Box>();
        int column = 0;
        for (int i = 0; i < maxSize; i++) {
            boxes.add(new Box(rowCount, column++));
            if (column == Config.getInstance().boxPerLine && i < (maxSize - 1)) {
                column = 0;
                rowCount++;
            }
        }
    }


    public void add(Element element) {
        if (isFull()) {
            ZLog.e(name + " is full. Can't add " + element.getName());
            return;
        }
        Box box = getBox(element);
        box.addElement(element);
    }


    public boolean isFull() {
        for (Box box : boxes) {
            if (box.isEmpty()) {
                return true;
            }
        }
        return false;
    }

    private Box getBox(Element element) {
        Box emptyBox = null;
        for (Box box : boxes) {
            if (box.contains(element)) {
                return box;
            }
            if (emptyBox != null && box.isEmpty()) {
                emptyBox = box;
            }
        }
        return emptyBox;
    }


    public String getName() {
        return name;
    }

    public List<Box> getBoxes() {
        return boxes;
    }

    public int getRowCount() {
        return rowCount;
    }
}
